$(function () {
    $("#jqGrid").Grid({
        url: '../back/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '用户编号', name: 'uid', index: 'uid', width: 30},
            {label: '手机号', name: 'phone', index: 'phone', width: 40},
            {label: '真实姓名', name: 'realName', index: 'realName', width: 40},
            {
                label: '证件类型', name: 'idCardType', index: 'idCardType', width: 30, formatter: function (value) {
                    if (value == '1')
                        return '身份证';
                    else
                        return '';
                }
            },
            {label: '证件号码', name: 'idCardNo', index: 'idCardNo', width: 90},
            {label: '银行卡号', name: 'cardNo', index: 'cardNo', width: 90},
            {label: '银行名称', name: 'cardBank', index: 'cardBank', width: 90},
            {label: '开户行分行', name: 'openBank', index: 'openBank', width: 90},
            {label: '预留手机', name: 'reservePhone', index: 'reservePhone', width: 40},
            {
                label: '创建日期', name: 'createDate', index: 'createDate', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            }]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        back: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            phone: '',
            uid: '',
            cardNo: '',
            idcard: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.back = {};
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.back.id == null ? "../back/save" : "../back/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.back),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../back/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../back/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.back = r.back;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'name': vm.q.name,
                    'phone': vm.q.phone,
                    'uid': vm.q.uid,
                    'cardNo': vm.q.cardNo
                    , 'idcard': vm.q.idcard
                },
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                phone: '',
                uid: '',
                cardNo: '',
                idcard: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});